const DB = require('../../../utils/db')
// 引入jwt token工具
const JwtUtil = require('../../../utils/jwt')
/**
 * POST
 * 创建一个群聊
 */
module.exports = async (ctx) => {
  let params = ctx.request.body
  // let res
  // 获取token
  let token = ctx.request.header.authorization
  let jwt = new JwtUtil(token)
  // 用token获取到userid
  let userid = jwt.verifyToken()
  let chars = [
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
  ]

  function generateMixed(n) {
    let res = ''
    for (let i = 0; i < n; i++) {
      let id = Math.ceil(Math.random() * 35)
      res += chars[id]
    }
    return res
  }
  let groupid = generateMixed( 5)
  // group是关键字
  const res = await DB.query(
    `insert into \`group\`(groupid, groupName, introduction, type, createTime,lord) values('${groupid}','${params.groupName}','${params.introduction}','${params.type}', now(),'${userid}')`
  )
  const res1 = await DB.query(
    `insert into user_group(userid,groupid,createTime) values('${userid}','${groupid}',now())`
  )
  ctx.body = {
    code: 0,
    data: res,
  }
}
